package com.clothing.customization.service.impl;

import com.clothing.customization.entity.DesignerQualification;
import com.clothing.customization.repository.DesignerQualificationRepository;
import com.clothing.customization.service.DesignerQualificationService;
import jakarta.persistence.criteria.Predicate;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class DesignerQualificationServiceImpl implements DesignerQualificationService {

    private final DesignerQualificationRepository qualificationRepository;

    @Override
    public Page<DesignerQualification> findByPage(Map<String, Object> params) {
        int page = Integer.parseInt(Objects.toString(params.getOrDefault("page", "1")));
        int size = Integer.parseInt(Objects.toString(params.getOrDefault("size", "10")));
        
        // 创建查询条件
        Specification<DesignerQualification> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            
            // 设计师姓名
            if (params.get("designerName") != null && !params.get("designerName").toString().isEmpty()) {
                predicates.add(cb.like(root.get("designer").get("realName"), "%" + params.get("designerName") + "%"));
            }
            
            // 资质类型
            if (params.get("qualificationType") != null && !params.get("qualificationType").toString().isEmpty()) {
                predicates.add(cb.equal(root.get("qualificationType"), params.get("qualificationType")));
            }
            
            // 状态
            if (params.get("status") != null && !params.get("status").toString().isEmpty()) {
                predicates.add(cb.equal(root.get("status"), Integer.parseInt(params.get("status").toString())));
            }
            
            return cb.and(predicates.toArray(new Predicate[0]));
        };
        
        // 创建分页请求
        PageRequest pageRequest = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createTime"));
        
        return qualificationRepository.findAll(spec, pageRequest);
    }

    @Override
    @Transactional
    public void approve(Long id) {
        DesignerQualification qualification = qualificationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("资质审核记录不存在"));
        
        qualification.setStatus(1); // 已通过
        qualification.setReviewTime(new Date());
        // TODO: 设置当前用户为审核人
        
        qualificationRepository.save(qualification);
    }

    @Override
    @Transactional
    public void reject(Long id) {
        DesignerQualification qualification = qualificationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("资质审核记录不存在"));
        
        qualification.setStatus(2); // 已拒绝
        qualification.setReviewTime(new Date());
        // TODO: 设置当前用户为审核人
        
        qualificationRepository.save(qualification);
    }
} 